﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    this->setCentralWidget(ui->stackedWidget);
    ui->stackedWidget->setCurrentWidget(ui->pagLog);
    this->resize(406,360);

    ui->toolBar->removeAction(ui->actUserManage);
    ui->toolBar->removeAction(ui->actWarehouse);

    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setDatabaseName("db_warehouse");
    m_db.setHostName("localhost");
    m_db.setUserName("root");
    m_db.setPassword("KWP-ffmxyr0");
    if(m_db.open() == false){
        QMessageBox::warning(0,"警告","数据库初始化失败.");
        return;
    }

    m_userModel = new QSqlTableModel(this);
    m_userModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_userModel->setTable("tb_user");
    m_userModel->select();
    m_userModel->setHeaderData(0,Qt::Horizontal,"编号");
    m_userModel->setHeaderData(1,Qt::Horizontal,"账号");
    m_userModel->setHeaderData(2,Qt::Horizontal,"密码");
    m_userModel->setHeaderData(3,Qt::Horizontal,"名字");

    m_whModel = new QSqlTableModel(this);
    m_whModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_whModel->setTable("tb_commodity");
    m_whModel->select();
    m_whModel->setHeaderData(0,Qt::Horizontal,"编号");
    m_whModel->setHeaderData(1,Qt::Horizontal,"名称");
    m_whModel->setHeaderData(2,Qt::Horizontal,"类型");
    m_whModel->setHeaderData(3,Qt::Horizontal,"品牌");
    m_whModel->setHeaderData(4,Qt::Horizontal,"价格");
    m_whModel->setHeaderData(5,Qt::Horizontal,"数量");

    ui->tvUser->setModel(m_userModel);
    ui->tvUser->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvWarehouse->setModel(m_whModel);
    ui->tvWarehouse->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    updateType();
}

void MainWindow::updateType()
{
    ui->combType->clear();
    ui->combType->addItem("所有类型");
    for(int i = 0;i < m_whModel->rowCount();++i){
        bool flag = false;
        for(int j = 0;j < ui->combType->count();++j){
            if(ui->combType->itemText(j) == m_whModel->data(m_whModel->index(i,2)).toString()){
                flag = true;
                break;
            }
        }
        if(flag == false){
            ui->combType->addItem(m_whModel->data(m_whModel->index(i,2)).toString());
        }
    }
}


void MainWindow::on_actUserManage_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagUser);
}


void MainWindow::on_actWarehouse_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagWh);
}


void MainWindow::on_btnAppendUser_clicked()
{
    m_userModel->insertRow(m_userModel->rowCount());
}


void MainWindow::on_btnDeleteUser_clicked()
{
    if(ui->tvUser->currentIndex().isValid()){
        m_userModel->removeRow(ui->tvUser->currentIndex().row());
    }
}


void MainWindow::on_btnResetUser_clicked()
{
    m_userModel->select();
}


void MainWindow::on_btnSubmitUser_clicked()
{
    if(m_userModel->isDirty()){
        if(m_userModel->submitAll() == true){
            QMessageBox::information(0,"提示","提交成功!");
        }
    }
}


void MainWindow::on_btnAppendWh_clicked()
{
    m_whModel->insertRow(m_whModel->rowCount());
}


void MainWindow::on_btnDeleteUserWh_clicked()
{
    if(ui->tvWarehouse->currentIndex().isValid()){
        m_whModel->removeRow(ui->tvWarehouse->currentIndex().row());
    }
}


void MainWindow::on_btnResetUserWh_clicked()
{
    m_whModel->select();
}


void MainWindow::on_btnSubmitUserWh_clicked()
{
    if(m_whModel->isDirty()){
        if(m_whModel->submitAll() == true){
            QMessageBox::information(0,"提示","提交成功!");
            updateType();
        }
    }
}


void MainWindow::on_btnLogin_clicked()
{
    QString account = ui->editAccount->text();
    QString pswd = ui->editPswd->text();
    if(account.isEmpty() || pswd.isEmpty()){
        QMessageBox::warning(0,"警告","请输入完整的账号密码.");
        return;
    }

    QSqlQuery query;
    query.exec("select * from tb_user where account='"+account+"';");
    query.next();
    if(!query.isValid()){
        QMessageBox::warning(0,"警告","该账号未注册.");
        return;
    }
    if(query.value("pswd").toString() != pswd){
        QMessageBox::warning(0,"警告","密码错误.");
        return;
    }

    this->resize(700,400);
    ui->toolBar->addAction(ui->actUserManage);
    ui->toolBar->addAction(ui->actWarehouse);
    ui->stackedWidget->setCurrentWidget(ui->pagUser);
}


void MainWindow::on_btnFind_clicked()
{
    QString findStr = ui->editFindName->text();
    if(findStr.isEmpty()){
        for(int i = 0;i < m_whModel->rowCount();++i){
            ui->tvWarehouse->showRow(i);
        }
    }else{

        for(int i = 0;i < m_whModel->rowCount();++i){
            ui->tvWarehouse->hideRow(i);
            for(int j = 0;j < m_whModel->columnCount();++j){
                if(m_whModel->data(m_whModel->index(i,j)).toString().contains(findStr)){
                    ui->tvWarehouse->showRow(i);
                    break;
                }
            }
        }

    }
}

void MainWindow::on_combType_currentTextChanged(const QString &arg1)
{
    if(arg1 == "所有类型"){
        for(int i = 0;i < m_whModel->rowCount();++i){
            ui->tvWarehouse->showRow(i);
        }
        return;
    }

    for(int i = 0;i < m_whModel->rowCount();++i){
        ui->tvWarehouse->hideRow(i);
        if(m_whModel->data(m_whModel->index(i,2)).toString() == arg1){
            ui->tvWarehouse->showRow(i);
        }
    }
}


void MainWindow::on_btnSell_clicked()
{
    Dialog d(m_whModel,this);
    int result = d.exec();
    if(result == QDialog::Accepted){
        m_whModel->select();
    }
}

